package com.huiminpay.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
@Slf4j
@Component
@RocketMQMessageListener(topic = "myTopic2",consumerGroup = "demo-consumer-group2")
public class ConsumerSimple2 implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt message) {
        System.out.println(LocalDateTime.now());
        System.out.println("收到消息"+message);

        byte[] body = message.getBody();
        String jsonStr = new String(body);

        // 重试次数
        int reconsumeTimes = message.getReconsumeTimes();
        log.info("当前重试次数{},接收消息为{}",reconsumeTimes,jsonStr);
        if(reconsumeTimes>2){
            return;
        }

        throw new RuntimeException("重试");
    }
}
